﻿using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;

namespace LetsDoIt.Controls
{
    public partial class TextBoxEx : UserControl
    {
        public new event EventHandler TextChanged;
        public TextBoxEx()
        {
            InitializeComponent();

            textBox1.BackColor = this.BackColor;
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        }

        void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.OnTextChanged(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            e.Graphics.Clear(this.Parent.BackColor);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (bufferimage == null)
                draw_buffered_image();
            e.Graphics.DrawImage(bufferimage, this.ClientRectangle, 0,
                                 0, this.Width, this.Height, GraphicsUnit.Pixel,
                                 imageattr);
        }


        private Image bufferimage = null;
        private ImageAttributes imageattr = new ImageAttributes();
        private Color chroma = Color.Transparent;
        public Color Chroma
        {
            get
            {
                return chroma;
            }
            set
            {
                chroma = value;
                bufferimage = null;
                this.Invalidate();
            }
        }

        private bool multiline;
        public bool Multiline
        {
            get { return multiline; }
            set
            {
                multiline = value;
                textBox1.Multiline = multiline;
                textBox1.ScrollBars = multiline ? ScrollBars.Vertical : ScrollBars.None;
                bufferimage = null;
                this.Invalidate();
            }
        }

        public override string Text
        {
            get { return textBox1.Text; }
            set
            {
                textBox1.Text = value;
            }
        }

        public char PasswordChar
        {
            get
            {
                return textBox1.PasswordChar;
            }
            set
            {
                textBox1.PasswordChar = value;
            }
        }

        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
                textBox1.BackColor = value;
                bufferimage = null;
                this.Invalidate();
            }
        }

        private int radius = 4;
        private int Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                bufferimage = null;
                this.Invalidate();
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            textBox1.Bounds = new Rectangle(radius, radius, this.Width - 2 * radius, this.Height - 2 * radius);
            bufferimage = null;
            this.Invalidate();
        }

        private void draw_buffered_image()
        {

            imageattr.SetColorKey(chroma, chroma);

            // build a new image buffer
            bufferimage = new Bitmap(this.Width, this.Height);
            Graphics graphs = Graphics.FromImage(bufferimage);

            //graphs.
            Brush buttonBrush = new SolidBrush(this.BackColor);
            Pen buttonPen = new Pen(this.BackColor, 1);

            //tmp image
            Bitmap img1 = new Bitmap(2 * radius + 1, 2 * radius + 1);
            using (Graphics g1 = Graphics.FromImage(img1))
            {
                g1.Clear(chroma);

                g1.DrawEllipse(buttonPen, 0, 0, 2 * radius, 2 * radius);
                g1.FillEllipse(buttonBrush, 0, 0, 2 * radius, 2 * radius);
                //corner top-left 
                Rectangle dr1 = new Rectangle(0, 0, radius, radius);
                graphs.DrawImage(img1, 0, 0, dr1, GraphicsUnit.Pixel);
                //corner top-right
                dr1 = new Rectangle(radius + 1, 0, radius, radius);
                graphs.DrawImage(img1, this.Width - radius, 0, dr1, GraphicsUnit.Pixel);
                //corner bottom-left
                dr1 = new Rectangle(0, radius + 1, radius, radius);
                graphs.DrawImage(img1, 0, this.Height - radius, dr1, GraphicsUnit.Pixel);
                //corner bottpm-left
                dr1 = new Rectangle(radius + 1, radius + 1, radius, radius);
                graphs.DrawImage(img1, this.Width - radius, this.Height - radius, dr1, GraphicsUnit.Pixel);
            }

            //top and bottom line + vertical filling
            graphs.DrawLine(buttonPen, radius, 0, this.Width - radius, 0);
            graphs.DrawLine(buttonPen, radius, this.Height - 1, this.Width - radius, this.Height - 1);
            graphs.FillPolygon(buttonBrush, new Point[]
                                                {
                                                    new Point( radius, 0),
                                                    new Point(this.Width - radius, 0),
                                                    new Point(this.Width - radius, this.Height-1),
                                                    new Point(radius, this.Height-1)
                                                });

            //left and right line + horisontal filling
            graphs.DrawLine(buttonPen, 0, radius, 0, this.Height - radius);
            graphs.DrawLine(buttonPen, this.Width - 1, radius, this.Width - 1, this.Height - radius);
            graphs.FillPolygon(buttonBrush, new Point[]
                                                {
                                                    new Point( 0, radius),	
                                                    new Point(0, this.Height - radius),
                                                    new Point(this.Width-1, this.Height-radius),
                                                    new Point(this.Width - 1, radius)
													
			                                		
                                                });

            // clean up resources
            buttonBrush.Dispose();
            buttonPen.Dispose();
            graphs.Dispose();
        }

    }
}